package net.sharpf2.api.game;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sharpf2.sfcore.client.Interface;
import net.sharpf2.api.Globals;
import net.sharpf2.api.Methods;
import net.sharpf2.api.types.Content;

/**
 *
 * @author Rick van Biljouw
 * @author Piotr Gajdowski
 */
public class Interfaces {
    private static String regexString;

    /**
     * Gets an interface.
     * @param   id0 Interface ID.
     * @param   id1 Child ID.
     * @return  Content<Interface>
     */
    public static Content<Interface> get(int id0, int id1) {
        if (Globals.CLIENT.getInterfaceCache()[id0][id1] != null) {
            return new Content<Interface>(Globals.CLIENT.getInterfaceCache()[id0][id1]);
        }
        return null;
    }

    /**
     * Gets an interface.
     * @param   id  Interface ID.
     * @return  Content<Interface>
     */
    public static Content<Interface> get(int id) {
        return get(id, 0);
    }

    /**
     * Gets the child of an interface's child.
     * @param id0       Parent interface ID.
     * @param id1       Child of parent interface.
     * @param childId   Child of parent's child interface.
     * @return          Content<Interface>
     */
    public static Content<Interface> getChild(int id0, int id1, int childId) {
        if (Globals.CLIENT.getInterfaceCache()[id0][id1] != null) {
            return new Content<Interface>(Globals.CLIENT.getInterfaceCache()[id0][id1].getChildren()[childId]);
        }
        return null;
    }
    
    /**
     * Gets all Interfaces.
     * @return Array of interfaces.
     */
    public static synchronized Interface[] getAll() {
	Interface[][] cache = Globals.CLIENT.getInterfaceCache();
	if (cache == null) {
            return new Interface[0];
	}
	final List<Content<Interface>> out = new ArrayList<Content<Interface>>();
	for (int i = 0; i < cache.length; i++) {
            if (cache[i] != null) {
		final Content<Interface> in = get(i);
                if (isInterfaceValid(i)) {
                    out.add(in);
                }
            }
	}
        return out.toArray(new Interface[out.size()]);
    }

    /**
     * Checks if specified interface is valid.
     * @param   id    Interface ID.
     * @return  True if is valid, otherwise false.
     */
    public static boolean isInterfaceValid(int id) {
        return Globals.CLIENT.getValidInterfaces()[id];
    }

    /**
     * Searches through every interface if any child contains the specified text
     * @param   regexString  Text to search for.
     * @return  Interface.
     */
    public static Content<Interface> getInterfaceByText(String regexString) {
        Interface[][] cache = Globals.CLIENT.getInterfaceCache();
        for (int i = 0; i < cache.length; i++) {
            if (cache[i] != null && Interfaces.isInterfaceValid(i)) {
                for (int j = 0; j < cache.length; j++) {
                    if (cache[i][j] != null && i != 137) {
                        Interface iface = cache[i][j];
                        Pattern regex = Pattern.compile(regexString, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
                        Matcher regexMatcher = regex.matcher(iface.getText());
                        if (regexMatcher.find() && !iface.isHidden()) {
                            System.out.println("Found Match : "+i+", "+j+", "+regexMatcher.toString());
                            return Interfaces.get(i, j);
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * Searches if any of the interfaces child contains the given action.
     * @param   s Action.
     * @param   ifaceID   Interface Parent ID.
     * @return  The child id with the containing action string.
     */
    public static int actionSearcher(String s, int ifaceID) {
        Interface[] iface = Globals.CLIENT.getInterfaceCache()[ifaceID];
        for (int i = 0; i < iface.length; i++) {
            Content<Interface> children = Interfaces.get(ifaceID, i);
            for (int j = 0; j < children.getContent().getActions().length; j++) {
                if (children.getContent().getActions()[j].contains(s)) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * Checks if chatbox contains Continue link.
     * @return True if contains Continue link, otherwise false.
     */
    public static boolean canContinue() {
        if (getInterfaceByText("(Click here to continue|Hier klicken, um fortzufahren|Cliquez ici pour continuer)") != null) {
            return true;
        }
        return false;
    }

    /**
     * Clicks Continue link.
     */
    public static void clickContinue() {
        if (canContinue()) {
            Content<Interface> ccontinue = getInterfaceByText("(Click here to continue|Hier klicken, um fortzufahren|Cliquez ici pour continuer)");
            if (ccontinue != null) {
                System.out.println("Continue Found !! "+ccontinue.getContent().getAbsoluteX()+", "+ccontinue.getContent().getAbsoluteY());
                ccontinue.getActions().click();
                Methods.sleep(200);
            }
        }
    }
    
}